import { Configuration, App } from '@midwayjs/core'
import * as koa from '@midwayjs/koa'
import * as staticFile from '@midwayjs/static-file'
import * as validate from '@midwayjs/validate'
import * as info from '@midwayjs/info'
import * as typegoose from '@midwayjs/typegoose'
import * as crossDomain from '@midwayjs/cross-domain'
import * as jwt from '@midwayjs/jwt'
import * as busboy from '@midwayjs/busboy'
import DefaultConfig from './config/config.default.js'
import UnittestConfig from './config/config.unittest.js'
import { DefaultErrorFilter } from './filter/default.filter.js'
// import { NotFoundFilter } from './filter/notfound.filter.js';
import { ResultMiddleware } from './middleware/result.middleware.js'
import { JwtMiddleware } from './middleware/jwt.middleware.js'
import { initServer } from './initServer.js'

// .ts引入启动报错

@Configuration({
  imports: [
    koa,
    jwt,
    validate,
    typegoose,
    staticFile,
    crossDomain,
    busboy,
    {
      component: info,
      enabledEnvironment: ['local']
    }
  ],
  importConfigs: [
    {
      default: DefaultConfig,
      unittest: UnittestConfig
    }
  ]
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application

  async onReady() {
    // add middleware
    this.app.useMiddleware([JwtMiddleware, ResultMiddleware])
    // add filter
    this.app.useFilter([DefaultErrorFilter])

    initServer()
  }
}
